<table id="dataTable"></table>

{include file="system@block/layui" /}

<script type="text/html" title="操作按钮模板" id="buttonTpl">
	{{# if(d.is_return == 0){ }}
	{{# if(d.checkcloth_id > 0){ }}
    	<a href="javascript:void(0)" data-url="{:url('checkclothShrink/returnClothNum')}?mid={{ d.order_material_id }}&id={{ d.checkcloth_id }}&from={$from}" class="layui-btn layui-btn-xs layui-btn-normal return-clothnum">整条退</a>
	{{# } else if(d.shrink_id > 0){ }}
		<a href="javascript:void(0)" data-url="{:url('checkclothShrink/returnClothNum')}?mid={{ d.order_material_id }}&id={{ d.shrink_id }}&from={$from}" class="layui-btn layui-btn-xs layui-btn-normal return-clothnum">整条退</a>
	{{# } else { }}
		<a href="javascript:void(0)" data-url="{:url('returnClothNum')}?model=return_clothnum&mid={{ d.order_material_id }}&id={{ d.id }}&from={$from}" class="layui-btn layui-btn-xs layui-btn-normal return-clothnum">整条退</a>
	{{# } }}
	<a href="javascript:void(0)" class="layui-btn layui-btn-xs layui-btn-warm return-length" data-id="{{ d.id }}">按米退</a>
  <a href="javascript:void(0)" data-url="{:url('delMakeTag')}?order_id={{d.order_id}}&mid={{ d.order_material_id }}&id={{ d.id }}" data-id="{{ d.id }}" class="layui-btn layui-btn-xs layui-btn-warm del-clothnum" >删除</a>
	{{# } }}
</script>
<div id="returnLengthTpl" style="display:none;">
	<form class="layui-form" action="">
		<div class="layui-form-item mt20">
		   <div class="layui-inline">
		     <label class="layui-form-label">米数</label>
		     <div class="layui-input-inline">
		       <input type="text" name="length" lay-verify="required|number" placeholder="请输入退料米数" autocomplete="off" class="layui-input">
		     </div>
		   </div>
		   <div class="layui-inline">
		     <label class="layui-form-label">退料类型</label>
		     <div class="layui-input-inline">
		     	{:Form::checkbox('reason', '', ['多料' => '多料', '批色不通过' => '批色不通过', '疵布'=>'疵布', '其它'=>'其它'])}
		     </div>
		   </div>
		</div>
	</form>
</div>

<div id="returnClothnumTpl" style="display:none;">
	<form class="layui-form" action="">
		<div class="layui-form-item mt20">
            <div class="layui-inline">
		     <label class="layui-form-label">公斤数</label>
		     <div class="layui-input-inline">
		       <input type="text" value="" name="weight" lay-verify="required|number" placeholder="请输入公斤数" autocomplete="off" class="layui-input billing-caigou-kgcount">
		     </div>
		   </div>
            <div class="layui-inline">
		     <label class="layui-form-label">米数</label>
		     <div class="layui-input-inline">
		       <input type="text" value="" name="length" lay-verify="required|number" placeholder="请输入退料米数" autocomplete="off" class="layui-input billingCaigouCount">
		     </div>
		   </div>
		   <div class="layui-inline">
		     <label class="layui-form-label">退料类型</label>
		     <div class="layui-input-inline">
		     	{:Form::checkbox('reason', '', ['多料' => '多料','克重' => '克重', '批色不通过' => '批色不通过', '疵布'=>'疵布', '其它'=>'其它'])}
		     </div>
		   </div>
		</div>
	</form>
</div>

<script type="text/javascript">
    layui.use(['table', 'tool', 'jquery', 'layer', 'form'], function() {
        var $ = layui.jquery;
        var layer = layui.layer;
        var table = layui.table;
        var form = layui.form;
        form.render(); 
        
        table.render({
            elem: '#dataTable'
            ,url: '{:url("", ["mid" => $mid, "model" => "get_list"])}' //数据接口
            ,page: false //开启分页
            ,skin: 'row'
            ,even: true
            ,limit: 20
            ,limits: [20,50,100,150,200]
            ,text: {
                none : '暂无相关数据'
            }
            ,cols: [[ //表头
                {field: 'name', title: '材料名称',templet:function(d){return d.mInfo.name;},width:180}
                ,{field: 'name', title: '颜色',templet:function(d){return d.mInfo.color;}}
                ,{field: 'stalls_name', title: '档口'}
                ,{field: 'cloth_num', title: '布号',width:70}
                ,{field: 'is_checkcloth', title: '验布',width:70, templet:function(d){return layui.tool.formatBool(d.is_checkcloth)}}
                ,{field: 'is_shrink', title: '缩水',width:70, templet:function(d){return layui.tool.formatBool(d.is_shrink)}}
                ,{field: 'is_return', title: '退料',width:70, templet:function(d){return layui.tool.formatBool(d.is_return)}}
                ,{title: '操作', templet: '#buttonTpl', fixed: 'right',width:200}
            ]]
        ,done: function (res) {
            var row=res.data[0];
            $(".billing-caigou-kgcount").on("input",function(e){
                var billingCaigouKgcount=e.delegateTarget.value;
                var fabricWidth = row.fabric_width;//幅宽
                var weight = row.weight;//克重
                var billingCaigouCount = layui.tool.bcdiv((billingCaigouKgcount*100000), fabricWidth*weight);//开单采购总数=(开单采购公斤数*100000)/幅宽 / 克重
                $('.billingCaigouCount').val(billingCaigouCount);
            });
        }
        });
        
        // 按米退料
        $(document).on('click','.return-length',function(){
        var id = $(this).attr('data-id');

        var layerReturnLength = layer.open({
          type: 1
          ,shade: 0
          ,offset: '100px'
          ,anim: parseInt(Math.random()*(6+1),10)
          ,skin:'layui-layer-molv'
          ,title:'按米退料'
          ,content: $('#returnLengthTpl')
          ,btn: ['确认']
          ,yes: function(index, layero){
            var length = $('#returnLengthTpl form input[name=length]').val();
            if(!(/((^[1-9]\d*)|^0)(\.\d{0,2}){0,1}$/.test(length))){
              layer.msg('米数输入错误', {icon: 2,anim: 6});
              return;
            }
              data = $('#returnLengthTpl form').serialize();
              if(data.indexOf('reason')==-1){
                    layer.msg('原因不能为空', {icon: 2,anim: 6});
                    return;
                }
              $.ajax({
               type: "POST",
               url: "{:url('returnClothnum', ['model' => 'return_length', 'mid' => $mid, 'from' => $from])}",
               data: data+"&id="+id,
               dataType:"json",
               success: function(res){
                if (res.code == 1) {
                 $('#returnLengthTpl form')[0].reset();
                 layer.close(layerReturnLength);
                 layer.msg(res.msg, {icon: 1,end:function(){table.reload('dataTable');}});
                } else {
                 layer.msg(res.msg, {icon: 2,anim: 6});
                }
               }
            });
          }
        });
      });
		
     	
        // 整条退料
        $(document).on('click','.return-clothnum',function(){
			var url = $(this).attr('data-url');
			
			var layerReturnLength = layer.open({
				type: 1
				,shade: 0
				,offset: '100px'
				,anim: parseInt(Math.random()*(6+1),10)
				,skin:'layui-layer-molv'
				,title:'整条退料'
				,content: $('#returnClothnumTpl')
				,btn: ['确认']
				,yes: function(index, layero){
			    	data = $('#returnClothnumTpl form').serialize();
                    if(data.indexOf('reason')==-1){
                        layer.msg('原因不能为空', {icon: 2,anim: 6});
                        return;
                    }
			    	$.ajax({
			 		   type: "POST",
			 		   url: url,
			 		   data: data,
			 		   dataType:"json",
			 		   success: function(res){
			 			  if (res.code == 1) {
			 				 $('#returnClothnumTpl form')[0].reset();
			 				 layer.close(layerReturnLength);
							 layer.msg(res.msg, {icon: 1,end:function(){table.reload('dataTable');}});
						  } else {
							 layer.msg(res.msg, {icon: 2,anim: 6});
						  }
			 		   }
			 		});
				}
			});
		});
        // 按米退料
        $(document).on('click','.del-clothnum',function(){
            var id = $(this).attr('data-id');
			layer.confirm('全部删除，重新打标', {btn: ['确定','取消']}, function(index){
				$.ajax({
					type: "POST",
					url: "{:url('delClothnumV2', [ 'mid' => $mid])}",
					data: "&id="+id,
					dataType:"json",
					success: function(res){
						if (res.code == 1) {
							layer.msg(res.msg, {icon: 1,end:function(){table.reload('dataTable');}});
						} else {
							layer.msg(res.msg, {icon: 2,anim: 6});
						}
					}
				});
			}, function(index){
				layer.close(index);
			});

        });
        
    });
</script>